using Microsoft.Extensions.Logging;
using Sage.Http.Configuration;
using Sage.Http.Core;
using Sage.Http.Options;
using System;

namespace Sage.Http.Factory
{
    /// <summary>
    /// HTTP请求管理器工厂（非DI环境）
    /// </summary>
    public static class HttpClientFactory
    {
        /// <summary>
        /// 创建HTTP请求管理器
        /// </summary>
        /// <param name="logger">日志记录器（可选）</param>
        /// <param name="retryOptions">重试选项（可选）</param>
        /// <param name="circuitBreakerOptions">熔断选项（可选）</param>
        /// <param name="loggingOptions">日志选项（可选）</param>
        /// <param name="rateLimitOptions">限流选项（可选）</param>
        /// <returns>HTTP请求管理器</returns>
        public static HttpRequestManager CreateManager(
            ILogger? logger = null,
            RetryOptions? retryOptions = null,
            CircuitBreakerOptions? circuitBreakerOptions = null,
            LoggingOptions? loggingOptions = null,
            RateLimitOptions? rateLimitOptions = null)
        {
            var httpClient = new HttpClient();
            return new HttpRequestManager(
                httpClient, 
                logger, 
                retryOptions ?? HttpClientGlobalConfig.GetDefaultRetryOptions(), 
                circuitBreakerOptions ?? HttpClientGlobalConfig.GetDefaultCircuitBreakerOptions(), 
                loggingOptions ?? HttpClientGlobalConfig.GetDefaultLoggingOptions(), 
                rateLimitOptions ?? HttpClientGlobalConfig.GetDefaultRateLimitOptions());
        }
        
        /// <summary>
        /// 创建HTTP请求管理器，使用基地址
        /// </summary>
        /// <param name="baseAddress">基地址</param>
        /// <param name="logger">日志记录器（可选）</param>
        /// <param name="retryOptions">重试选项（可选）</param>
        /// <param name="circuitBreakerOptions">熔断选项（可选）</param>
        /// <param name="loggingOptions">日志选项（可选）</param>
        /// <param name="rateLimitOptions">限流选项（可选）</param>
        /// <returns>HTTP请求管理器</returns>
        public static HttpRequestManager CreateManager(
            string baseAddress,
            ILogger? logger = null,
            RetryOptions? retryOptions = null,
            CircuitBreakerOptions? circuitBreakerOptions = null,
            LoggingOptions? loggingOptions = null,
            RateLimitOptions? rateLimitOptions = null)
        {
            if (string.IsNullOrEmpty(baseAddress))
            {
                throw new ArgumentException("基地址不能为空", nameof(baseAddress));
            }
            
            var httpClient = new HttpClient
            {
                BaseAddress = new Uri(baseAddress)
            };
            
            return new HttpRequestManager(
                httpClient, 
                logger, 
                retryOptions ?? HttpClientGlobalConfig.GetDefaultRetryOptions(), 
                circuitBreakerOptions ?? HttpClientGlobalConfig.GetDefaultCircuitBreakerOptions(), 
                loggingOptions ?? HttpClientGlobalConfig.GetDefaultLoggingOptions(), 
                rateLimitOptions ?? HttpClientGlobalConfig.GetDefaultRateLimitOptions());
        }
    }
}